package cn.e2win.biz.user;

import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysCompany;
import cn.e2win.base.entity.SysDict;
import cn.e2win.base.entity.SysSeq;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.entity.SysUserRole;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.security.UserSession;


@Service
public class CompanyService extends BaseService{
	

	@Autowired
	private CompanyDao companyDao;

	@Override
	public BaseDAO initDao() {
		return companyDao;
	}
	
	public void pagerCompany(Pager pager){
		companyDao.pagerCompany(pager);
	}


	@Transactional(rollbackFor=Exception.class)
	public SysCompany editCompany(SysCompany company) throws MyException {
		Integer currentUserId = UserSession.getUser().getId();
		Timestamp now = new Timestamp(System.currentTimeMillis());
		//1，添加公司
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("accountPrefix", company.getAccountPrefix());
		List<Map<String, Object>> list = companyDao.getListBySqlAutoParam("SELECT * FROM Sys_company where 1=1", map);
		if(list!=null && list.size()>1){
			throw new MyException("帐号前缀\"" + company.getAccountPrefix() + "\"已经被【" + list.get(0).get("coName") + "】占用!");
		}
		company.setCreateById(currentUserId);
		company.setCreateDate(now);
		company = (SysCompany)companyDao.merge(company);

		//2，添加老板登录帐号
		SysUser user = new SysUser(company);

		/*
		List<SysRole> roleList = new ArrayList<SysRole>();
		SysRole role = (SysRole)companyDao.getEntity(SysRole.class, 1);//默认老板权限
		
		roleList.add(role);
		user.setRoles(roleList);
		*/			
		user.setCreateById(currentUserId);
		user.setCreateDate(now);
		user = (SysUser)companyDao.merge(user);
		//设置老板帐号的权限

		SysUserRole userRole = new SysUserRole();
		userRole.setRoleId(-2);//普通管理员
		userRole.setUserId(user.getId());
		companyDao.merge(userRole);
/*		
		//3，添加成为我的客户
		Payment myCustomer = new Payment(company);
		myCustomer.setCoId(0);
		myCustomer.setCreateById(user.getId());
		myCustomer.setCreateDate(now);
		myCustomer = (Payment)companyDao.merge(myCustomer);
		
		//4，添加一个散客客户
		Payment customer = new Payment();
		customer.setCoId(company.getId());
		customer.setCustName("散客");
		customer.setCreateById(user.getId());
		customer.setCreateDate(now);
		customer = (Payment)companyDao.merge(customer);
		companyDao.updateBySql("UPDATE crm_customer SET id=" + (-1* company.getId()) + " WHERE id=" + customer.getId() ,null);
*/		
		//5,初始化单号等
		SysSeq xs = new SysSeq(company.getId(),"PA","付款申请单");
		companyDao.merge(xs);
		
		SysSeq js = new SysSeq(company.getId(),"LA","贷款申请单");
		companyDao.merge(js);
/*
		SysSeq sk = new SysSeq(company.getId(),"SK","收款单");
		companyDao.merge(sk);
		
		SysSeq zc = new SysSeq(company.getId(),"ZC","支出单");
		companyDao.merge(zc);*/
		
		
		return company;
	}

	public SysCompany editBaseInfo(SysCompany obj) {
		obj = (SysCompany)companyDao.merge(obj);
		return obj;
	}

	
	
	
	public List<Map> dictTree() throws SQLException{
		List<Map> list = new ArrayList();


		Pager pager = new Pager();
		pager.setReturnAll(true);
		pager.getScMap().put("editAbleInt", "1");
		companyDao.pagerDict(pager);
		Integer bigId = null;
		Map<String, Object> rootMap = null;
		for(Map<String, Object> item : pager.getResultList()){
			if(!item.get("bigId").equals(bigId)){
				bigId = (Integer)item.get("bigId");
				rootMap = new HashMap<String, Object>();
				rootMap.put("id", bigId);
				rootMap.put("key", item.get("bigKey"));
				rootMap.put("value", item.get("bigValue"));
				rootMap.put("list", new ArrayList<Map<String, Object>>());
				list.add(rootMap);
			}
			
			
/*			Map itemMap = new HashMap();
			itemMap.put("id", item.get("id"));
			itemMap.put("key", item.get("myKey"));
			itemMap.put("value", item.get("myValue"));
*/			((List<Map<String, Object>>)rootMap.get("list")).add(item);			
		}
		
		
		
		return list;
	}

	public void pagerDict(Pager pager)throws SQLException{
		companyDao.pagerDict(pager);
	}

	public void editDict(SysDict obj) throws MyException{
		obj.setMyKey(""+(companyDao.getDictTypeCount(obj.getMyType()) + 1));
		super.edit(obj);
	}
	
}
